<?php
/*
 * Created on 08.06.2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class DocxTemplateEditor
 {
 	private $extension = "document.xml";
 	private $delimiter = "-#-";
 	private $download_extension = "docx";
    private $files_to_edit;
    private $fields_to_edit_names;
 	private $values;
 	
 	public function init($working_dir)
 	{
 	  $this->files_to_edit = array();
 	  $this->fields_to_edit = array();
      $this->initFileList($working_dir);
      $this->initFieldsToEditNames();
 	}
 	
 	private function initFileList($working_dir)
 	{
      $dir_listing = dir($working_dir);
      while(($dir_entry = $dir_listing->read()) !== false)
	    {
	      if (($dir_entry != ".") && ($dir_entry != ".."))
	      {
	        if (is_file($working_dir.'/'.$dir_entry))
	        {
		       if (preg_match('/'.$this->extension.'$/',$dir_entry))
		        {
		        	array_push($this->files_to_edit,$dir_listing->path.$dir_entry);
		        }
	        }
	        else if (is_dir($working_dir.'/'.$dir_entry))
	        {
	            $this->initFileList($working_dir.'/'.$dir_entry.'/');
	        }
	      }
	    }
	 }
 	
 	public function replaceFields($values)
 	{
	    foreach($this->files_to_edit as $currentfile)
	    {
	      $content = file_get_contents($currentfile);
	      foreach($this->getFieldsToEdit() as $currentreplace)
	      {
	        $content = preg_replace('/'.$this->delimiter.$currentreplace["name"].$this->delimiter.'/',$values[$currentreplace["form_name"]],$content);     	
	        $this->values[$currentreplace["name"]] = $values[$currentreplace["form_name"]];
	      }
	      file_put_contents($currentfile,$content);
	    }
	    
 	}
 	
 	public function getFilesToEdit()
 	{
 		return $this->files_to_edit;
 	}
 
 	public function initFieldsToEditNames()
 	{
 		  $array_fields_to_replace = array();
 		  $array_fields_to_replace_names = array();
          for($i=0; $i<count($this->files_to_edit); $i++)	
          {
          	$file_content = file_get_contents($this->files_to_edit[$i]);
          	//	      echo "<textarea cols=\"50\" rows=\"60\">".$file_content."</textarea>";
          	
            $matches_array = array();
         	//preg_match_all('/#([^#]*)#/',$file_content,&$matches_array);
         	preg_match_all('/'.$this->delimiter.'([^'.$this->delimiter.']*)'.$this->delimiter.'/',$file_content,&$matches_array);
         	$unique_matches = array_unique($matches_array[1]);
         	foreach($unique_matches as $entry)
         	{
         		array_push($array_fields_to_replace_names, $entry);
         	}
          }
          $array_fields_to_replace_names = array_unique($array_fields_to_replace_names);
          $this->fields_to_edit_names = $array_fields_to_replace_names;

 	}
 	
 	public function getFieldsToEdit()
 	{
		  $array_fields_to_replace = array();    
	      $i = 0;
	      foreach ($this->fields_to_edit_names as $entry)
	      {
	     		$fieldinfo["form_name"] = "template_edit_".$i;
	     		$fieldinfo["name"] = $entry;
	     		$fieldinfo["value"] = $this->values[$entry];
	     		array_push($array_fields_to_replace, $fieldinfo);
	            $i++;
	      }
 	    return $array_fields_to_replace;
 	}
 	
 	public function getDownloadExtension()
 	{
 	    return $this->download_extension;
 	}
 }
 
?>
